﻿@using System.ComponentModel.DataAnnotations;
@using System.Text.Json;

<Form Model="@model" Name="form1" Method="post"
      LabelColSpan="8"
      WrapperColSpan="16"
      OnFinish="OnFinish" 
      OnFinishFailed="OnFinishFailed">
    <FormItem Label="Username" >
        <Input @bind-Value="@model.Username"/>
    </FormItem>
    <FormItem Label="Password">
        <InputPassword @bind-Value="@model.Password" />
    </FormItem>
    <FormItem WrapperColOffset="8" WrapperColSpan="16">
        <Checkbox @bind-Value="model.RememberMe">Remember me</Checkbox>
    </FormItem>
    <FormItem WrapperColOffset="8" WrapperColSpan="16">
        <Button Type="@ButtonType.Primary" HtmlType="submit">
            Submit
        </Button>
    </FormItem>
</Form>

@code
{
#if NET8_0_OR_GREATER
    [SupplyParameterFromForm(FormName = "form1")]
#endif
    public Model model { get; set; } = new();
    
    public class Model
    {
        [Required]
        public string Username { get; set; }
        [Required]
        public string Password { get; set; }
        public bool RememberMe { get; set; } = true;
    }

    private void OnFinish(EditContext editContext)
    {
        Console.WriteLine($"Success:{JsonSerializer.Serialize(model)}");
    }

    private void OnFinishFailed(EditContext editContext)
    {
        Console.WriteLine($"Failed:{JsonSerializer.Serialize(model)}");
    }
}